package com.pan.games.threadgame;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * 新建一个线程的三种方式
 * @author by panstark
 * @description
 * @notice
 * @date 2020/12/15
 */
@Slf4j
public class StartThread {


    public static void main(String[] args) {
        Thread thread = getAthread();
        log.info("thread:"+thread.getName());

        Thread waterThread = new WaterThread();
        waterThread.start();

        //方法一 实现runnable接口
        RunnableEmployee runnableEmployee =  new RunnableEmployee();
        Thread runableThread = new Thread(runnableEmployee);
        runableThread.start();

        //方法二 继承Thread类
        Thread extendsEmployee = new ExtendsEmployee();
        extendsEmployee.start();

        //方法三 写一个匿名内部类
        Thread thread1 = new Thread(
                new Runnable(){
            @Override
            public void run() {
                System.out.println("what do you want to do!");
            }
        });
        thread1.start();
    }


    public static Thread getAthread(){

        log.info("currentThread : "+Thread.currentThread().getName());

        Thread thread = new Thread();

        thread.start();

        log.info("you have a thread and his name is："+ thread.getName());

        log.info("you have a thread,but he has nothing to do.");

        return thread;
    }
}

//1):定义一个类A实现于java.lang.Runnable接口,注意A类不是线程类.
@Slf4j
class RunnableEmployee implements Runnable{
    //在A类中覆盖Runnable接口中的run方法.
    @Override
    public void run() {
        log.info("I want you earn money for me forever.");
    }
}

@Slf4j
class ExtendsEmployee extends Thread{
    //在A类中覆盖Runnable接口中的run方法.
    @Override
    public void run() {
        log.info("I want you earn money for me forever.");
    }
}

